﻿@namespace Blazorise.Docs.Docs.Examples

<Markdown ImageUploadChanged="@OnImageUploadChanged"
          ImageUploadStarted="@OnImageUploadStarted"
          ImageUploadProgressed="@OnImageUploadProgressed"
          ImageUploadEnded="@OnImageUploadEnded" />

@code {
    async Task OnImageUploadChanged( FileChangedEventArgs e )
    {
        try
        {
            foreach ( var file in e.Files )
            {
                using ( var stream = new System.IO.MemoryStream() )
                {
                    await file.WriteToStreamAsync( stream );

                    // do something with the stream
                }
            }
        }
        catch ( Exception exc )
        {
            Console.WriteLine( exc.Message );
        }
        finally
        {
            this.StateHasChanged();
        }
    }

    Task OnImageUploadStarted( FileStartedEventArgs e )
    {
        Console.WriteLine( $"Started Image: {e.File.Name}" );

        return Task.CompletedTask;
    }

    Task OnImageUploadProgressed( FileProgressedEventArgs e )
    {
        Console.WriteLine( $"Image: {e.File.Name} Progress: {(int)e.Percentage}" );

        return Task.CompletedTask;
    }

    Task OnImageUploadEnded( FileEndedEventArgs e )
    {
        // We need to report back to Markdown that upload is done. We do this by setting the UploadUrl.
        // NOTE: Since we're faking the upload in this demo we will just set some dummy UploadUrl.
        e.File.UploadUrl = "https://images.pexels.com/photos/4966601/pexels-photo-4966601.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=200";

        Console.WriteLine( $"Finished Image: {e.File.Name}, Success: {e.Success}" );

        return Task.CompletedTask;
    }
}